package com.easyticket.system.action;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.easyticket.system.pojo.User;

import com.easyticket.system.service.UserService;

import com.opensymphony.xwork2.ActionSupport;
import com.easyticket.common.util.MD5Code;

@Controller("userInfoAction")
@Scope("prototype")
public class UserInfoAction extends ActionSupport {

	protected Logger log = LoggerFactory.getLogger(getClass());
	
	@Resource(name = "userService")
	private UserService userService;

	private String userName;
	private String password;
	
	@Override
	public String execute() throws Exception {
//		MD5Code md5 = new MD5Code();
//		User user = new User();
//        user.setUserName(userName);
//        user.setPassword(md5.getMD5ofStr(password));
//        if (userService.checkLogin(user)) {
//        	return SUCCESS;
//        } 
//       
//        return INPUT;
		
		HttpServletResponse response = ServletActionContext.getResponse(); 
		MD5Code md5 = new MD5Code();
		User user = new User();
		user.setUserName(userName);
		user.setPassword(md5.getMD5ofStr(password));
		System.out.println("==========="+userName);
		System.out.println("==========="+password);
		if (userService.checkLogin(user)) {
			print(response, "success");
		} else {
			print(response, "input");
		}
		return null;
	}
	
	public String register() throws Exception {
		//String str2 = "402880e4230a259101230a27b21c0002";
		//String str1 = "402880e4230e6f1801230e74e76a0001";
		//testUserService.updateTransfer(str1, str2);
		MD5Code md5 = new MD5Code();
		User user = new User();
		user.setUserName(userName);
		user.setPassword(md5.getMD5ofStr(password));
		
		userService.save(user);
		return SUCCESS;
	}
	
	/**
	 * 读取请求参数转换JSONObject对象
	 *
	 * @param request HttpServletRequest 对象
	 * @return json格式的String对象
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	protected JSONObject readJson(HttpServletRequest request) throws Exception {
		JSONObject jsonObject = new JSONObject();
		try {
			Map parameterMap = request.getParameterMap();
	        // 通过循环遍历的方式获得key和value并set到JSONObject中
			Iterator paIter = parameterMap.keySet().iterator();
			while (paIter.hasNext()) {
				String key = paIter.next().toString();
				String[] values = (String[])parameterMap.get(key);
				jsonObject.accumulate(key, values[0]);
			}
			log.debug("从客户端获得json=" + jsonObject.toString());
		} catch (Exception e) {
			log.error("获取json数据出错，错误信息如下：nt" + e.getMessage());
			e.printStackTrace();
			throw e;
		}
		return jsonObject;
	}
	
	protected void print(HttpServletResponse response, String info) throws IOException {
		try {
			response.getWriter().print(info);
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		}
	}

	public Logger getLog() {
		return log;
	}

	public void setLog(Logger log) {
		this.log = log;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
